package btanalyzer.db.analyzer;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.gudy.azureus2.core3.torrent.TOTorrent;
import org.gudy.azureus2.core3.torrent.TOTorrentAnnounceURLSet;
import org.gudy.azureus2.core3.torrent.TOTorrentException;
import org.gudy.azureus2.core3.torrent.TOTorrentFactory;
import org.gudy.azureus2.core3.torrent.TOTorrentFile;
import org.gudy.azureus2.core3.util.BEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sun.jndi.ldap.BerEncoder;

import btanalyzer.db.dao.AnnounceUrlDao;
import btanalyzer.db.dao.MetadataDao;
import btanalyzer.db.dao.TorrentDao;
import btanalyzer.db.model.AnnounceUrl;
import btanalyzer.db.model.Metadata;
import btanalyzer.db.model.MetadataAnnounceUrl;
import btanalyzer.db.model.ResourceFile;
import btanalyzer.db.model.Torrent;

@Component
public class MetadataAnalyzerImpl implements MetadataAnalyzer {
	
	private static final String[] badSuffs = { 
		"WinZip_PRO_v12.1___Serials_By_ChattChitto.4894950.TPB.torrent", 
		"LimeWire_PRO_v5.5.8_Final___Crack_[ChattChitto_RG].5484559.TPB.torrent",
		"ConvertXtoDVD_v4.0.9.322___Serials_By_ChattChitto.5209454.TPB.torrent",
		"WinZip_PRO_FINAL_v14.5___Serials_[ChattChitto_RG].5498707.TPB.torrent",
		"WinRAR_v3.90_Final___KeyReg_By_ChattChitto.5069577.TPB.torrent",
		"Counter-Strike_Source_Patch_v1.0.0.64__AutoUpdate.6523747.TPB.torrent",
		"Metin2Ddmt2.6254663.TPB.torrent",
		"DriverPack_Solution_10.6_Solution_x32-64_[_Victory]_.5872740.TPB.torrent",
		"Everest_Ultimate_Edition_5.50.2100___Serial_[TrT-TcT].6112066.TPB.torrent",
		"TeamViewer_6.0.9699___Crack_[__work_100__]__NeW___[_ThumperTM_].5965248.TPB.torrent",
		"Microsoft_Office_2007_-_Product_key_Serial.4422784.TPB.torrent",
		"CyberLink_powerdirector_9_with_key_by_TheAaax9.6037069.TPB.torrent",
		"FM11_Metallic_Logos.zip.5939228.TPB.torrent",
		"Game_of_Thrones_S01E07_HDTV_XviD-ASAP_[eztv].6433333.TPB.torrent"
};
	
	private static final Logger log = LoggerFactory.getLogger(MetadataAnalyzerImpl.class);

	@Override
	public void run() {
		
		// get torrent without analyzed meta-data
		
		List<Torrent> torrents = torrentsByMetadataIsNull();
		System.out.println("ile takich torrentow=" + torrents.size());
		
		
		// analyzer meta-data for each torrent
		
		for (Torrent t : torrents) {
			getMetadataAnalyzerByTorrentId().analyzeMatadata(t.getId(), badSuffs);
		}
		
	}
	
	@Override
	@Transactional
	public List<Torrent> torrentsByMetadataIsNull() {
		return getTorrentDao().selectByMetadataIsNull();
	}

	@Autowired
	private TorrentDao torrentDao;
	public void setTorrentDao(TorrentDao torrentDao) {
		this.torrentDao = torrentDao;
	}
	public TorrentDao getTorrentDao() {
		return torrentDao;
	}
	
	@Autowired
	AnnounceUrlDao announceUrlDao;
	public void setAnnounceUrlDao(AnnounceUrlDao announceUrlDao) {
		this.announceUrlDao = announceUrlDao;
	}
	public AnnounceUrlDao getAnnounceUrlDao() {
		return announceUrlDao;
	}
	
	@Autowired
	MetadataDao metadataDao;
	public void setMetadataDao(MetadataDao metadataDao) {
		this.metadataDao = metadataDao;
	}
	public MetadataDao getMetadataDao() {
		return metadataDao;
	}
	
	@Autowired
	private MetadataAnalyzerByTorrentId metadataAnalyzerByTorrentId;
	public void setMetadataAnalyzerByTorrentId(
			MetadataAnalyzerByTorrentId metadataAnalyzerByTorrentId) {
		this.metadataAnalyzerByTorrentId = metadataAnalyzerByTorrentId;
	}
	public MetadataAnalyzerByTorrentId getMetadataAnalyzerByTorrentId() {
		return metadataAnalyzerByTorrentId;
	}
}
